<?php

import('ORG.Util.String');
/*
 * 加密相关函数
*/
class Encryption {
	/*
	 * 密码需要先Base64解密，再使用md5加密
	*/
	public static function password($password) {
		return md5(C('ENCRYPTION_KEY').base64_decode($password).C('ENCRYPTION_KEY'));
	}
	
	/*
	 * 获取uuid
	 */
	public static function uuid() {
		return md5(String::uuid());
	}
	
	/*
	 * 获取客户uuid，限长6位
	 */
	public static function c_uuid() {
		$count = 0;
		$customerModel = D('customer');
		do {
			$uuid = md5(String::uuid());
			$uuid = substr($uuid, 0, 6);
			
			$where['uuid'] = $uuid;
			$count = $customerModel->field('uuid')->where($where)->count();
		} while ($count > 0);
		
		return $uuid;
	}
	
	/*
	 * 获取用户token
	 */
	public static function token() {
		$token = md5(C('ENCRYPTION_KEY').String::uuid().C('ENCRYPTION_KEY'));
		return substr($token, 0, 8);
	}
	
}